/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-05 Marco Riccardi - initial implementation
 *  2011-03-25 Marco Riccardi - implementation of visitor pattern
 *
 */
package org.mizartools.system.xml;

import java.util.LinkedList;

import org.mizartools.system.ElementParseException;
import org.mizartools.system.IElement;
import org.mizartools.system.INode;
import org.mizartools.utility.xml.XMLAttribute;
import org.mizartools.utility.xml.XMLElement;

public class PerCasesReasoning implements IElement {

    public static final String NAME = "PerCasesReasoning";

    private INode parentNode;
    private Integer line = null;
    private Integer col = null;

    private boolean isBlockThesisFirst = false;
    
    private BlockThesis blockThesis = null;
    private LinkedList<CaseBlock> caseBlockList = new LinkedList<CaseBlock>();
    private LinkedList<SupposeBlock> supposeBlockList = new LinkedList<SupposeBlock>();
    private PerCases perCases = null;
    private Thesis thesis = null;
    private EndPosition endPosition = null;

    private PerCasesReasoning(INode parentNode){
        this.parentNode = parentNode;
    }

    public INode getParentNode() {
        return parentNode;
    }

    public static PerCasesReasoning newInstance(INode parentNode, LinkedList<XMLElement> xmlElementList) throws ElementParseException {
        PerCasesReasoning perCasesReasoning = new PerCasesReasoning(parentNode);
        XMLElement xmlElement = xmlElementList.poll();
        if (!xmlElement.getName().equals(NAME)){
            throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "XMLElement is a " + xmlElement.getName() + " not a " + NAME, xmlElementList);
        }

        for (XMLAttribute xmlAttribute : xmlElement.getXMLAttributeList()){
        	if (xmlAttribute.getName().equals("line")){
                if (perCasesReasoning.line == null){
                    try {perCasesReasoning.line = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
        	} else 
        	if (xmlAttribute.getName().equals("col")){
                if (perCasesReasoning.col == null){
                    try {perCasesReasoning.col = Integer.parseInt(xmlAttribute.getValue());}
                    catch (Exception e) {throw new ElementParseException(ElementParseException.NOT_VALID_TYPE_OF_ATTRIBUTE_ELEMENT, "XMLElement has an attribute " + xmlAttribute.getName() + " with an invalid type", xmlElementList);};
                } else {
                    throw new ElementParseException(ElementParseException.DUPLICATE_ATTRIBUTE_ELEMENT, "XMLElement has a duplicate attribute " + xmlAttribute.getName(), xmlElementList);
                } 
        	} else 
        		throw new ElementParseException(ElementParseException.NOT_VALID_ATTRIBUTE_ELEMENT, "XMLElement has not a valid attribute " + xmlAttribute.getName(), xmlElementList);
        }
        
        if (!xmlElement.isEmpty()){
        	boolean isFirst = true;
            while (true){
                xmlElement = xmlElementList.peek();
                if (xmlElement == null){
                    throw new ElementParseException(ElementParseException.UNEXPECTED_END_OF_FILE, "Unexpected end of XMLElement list");
                }
                if (xmlElement.getName().equals(NAME) && xmlElement.isEndElement()){
                    xmlElement = xmlElementList.poll();
                    break;
                }
                if (isFirst) {
                    if (xmlElement.getName().equals("BlockThesis")){
                    	perCasesReasoning.isBlockThesisFirst = true;	
                    }
                	isFirst = false;
                }
                if (xmlElement.getName().equals("BlockThesis")){
                    if (perCasesReasoning.blockThesis == null) {
                        perCasesReasoning.blockThesis = BlockThesis.newInstance(perCasesReasoning, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <BlockThesis> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("CaseBlock")){
                    perCasesReasoning.caseBlockList.add(CaseBlock.newInstance(perCasesReasoning, xmlElementList));
                } else if (xmlElement.getName().equals("SupposeBlock")){
                    perCasesReasoning.supposeBlockList.add(SupposeBlock.newInstance(perCasesReasoning, xmlElementList));
                } else if (xmlElement.getName().equals("PerCases")){
                    if (perCasesReasoning.perCases == null) {
                        perCasesReasoning.perCases = PerCases.newInstance(perCasesReasoning, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <PerCases> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("Thesis")){
                    if (perCasesReasoning.thesis == null) {
                        perCasesReasoning.thesis = Thesis.newInstance(perCasesReasoning, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <Thesis> in element " + NAME, xmlElementList);
                    }
                } else if (xmlElement.getName().equals("EndPosition")){
                    if (perCasesReasoning.endPosition == null) {
                        perCasesReasoning.endPosition = EndPosition.newInstance(perCasesReasoning, xmlElementList);
                    } else { 
                        throw new ElementParseException(ElementParseException.DUPLICATE_ELEMENT, "XMLElement list contains a duplicated <EndPosition> in element " + NAME, xmlElementList);
                    }
                } else 
                    throw new ElementParseException(ElementParseException.NOT_VALID_ELEMENT, "The XMLElement " + xmlElement.getName() + " is not valid in " + NAME, xmlElementList);
            }
        }

        return perCasesReasoning;
    }

    public LinkedList<XMLElement> getXMLElementList() {
        LinkedList<XMLElement> xmlElementList = new LinkedList<XMLElement>();
        XMLElement xmlElement = new XMLElement(NAME);
        if (this.line != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("line", this.line);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        if (this.col != null) {
            XMLAttribute xmlAttribute = new XMLAttribute("col", this.col);
            xmlElement.getXMLAttributeList().add(xmlAttribute);
        }
        boolean isEmpty = !(blockThesis != null
         || !caseBlockList.isEmpty() || !supposeBlockList.isEmpty()
         || perCases != null || thesis != null
         || endPosition != null);
        xmlElement.setEmpty(isEmpty);
        xmlElementList.add(xmlElement);
        if (this.isBlockThesisFirst){
            if (this.blockThesis != null){
                xmlElementList.addAll(this.blockThesis.getXMLElementList());
            }
            for (CaseBlock caseBlock : this.caseBlockList){
                xmlElementList.addAll(caseBlock.getXMLElementList());
            }
            for (SupposeBlock supposeBlock : this.supposeBlockList){
                xmlElementList.addAll(supposeBlock.getXMLElementList());
            }
            if (this.perCases != null){
                xmlElementList.addAll(this.perCases.getXMLElementList());
            }
            if (this.thesis != null){
                xmlElementList.addAll(this.thesis.getXMLElementList());
            }
            if (this.endPosition != null){
                xmlElementList.addAll(this.endPosition.getXMLElementList());
            }
        } else {
            for (CaseBlock caseBlock : this.caseBlockList){
                xmlElementList.addAll(caseBlock.getXMLElementList());
            }
            for (SupposeBlock supposeBlock : this.supposeBlockList){
                xmlElementList.addAll(supposeBlock.getXMLElementList());
            }
            if (this.perCases != null){
                xmlElementList.addAll(this.perCases.getXMLElementList());
            }
            if (this.endPosition != null){
                xmlElementList.addAll(this.endPosition.getXMLElementList());
            }
            if (this.blockThesis != null){
                xmlElementList.addAll(this.blockThesis.getXMLElementList());
            }
        }
        if (!isEmpty) { 
            xmlElement = new XMLElement(NAME);
            xmlElement.setEndElement(true);
            xmlElementList.add(xmlElement);
        }
        return xmlElementList;
    }

    public Integer getLine(){
        return line;
    }

    public Integer getCol(){
        return col;
    }

    public BlockThesis getBlockThesis(){
        return blockThesis;
    }

    public LinkedList<CaseBlock> getCaseBlockList(){
        return caseBlockList;
    }

    public LinkedList<SupposeBlock> getSupposeBlockList(){
        return supposeBlockList;
    }

    public PerCases getPerCases(){
        return perCases;
    }

    public Thesis getThesis(){
        return thesis;
    }

    public EndPosition getEndPosition(){
        return endPosition;
    }

	@Override
	public void accept(Visitor visitor) {
		if (visitor.visit(this)) {
			for(CaseBlock caseBlock: this.caseBlockList) {
				caseBlock.accept(visitor);
			}
			for(SupposeBlock supposeBlock: this.supposeBlockList) {
				supposeBlock.accept(visitor);
			}
		}
		visitor.endVisit(this);
	}

	@Override
	public String toString(){
		ToStringVisitor toStringVisitor = new ToStringVisitor();
		this.accept(toStringVisitor);
		return toStringVisitor.toString();
	}

}
